/*
 * Stok3d - getting you stoked on Flash Player native 3D
 * Copyright (C) 2009  David Lenaerts
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author information:
 * David Lenaerts
 * url: http://www.derschmale.com
 * mail: david.lenaerts@gmail.com
 * 
 */

package com.derschmale.stok3d.filters
{
	import flash.display.BitmapData;
	import flash.display.Shader;
	

	/**
	 * The EnvMapFilter class creates a cubic environment mapping effect on a DisplayObject.
	 * 
	 * @author David Lenaerts
	 * http://www.derschmale.com
	 */
	public class EnvMapFilter extends PixelShader3D
	{
		[Embed(source="/../pbj/CubicEnvNormalMapShader.pbj", mimeType="application/octet-stream")]
		private var NormalMapKernel : Class;
		
		[Embed(source="/../pbj/CubicEnvMapShader.pbj", mimeType="application/octet-stream")]
		private var FlatKernel : Class;
		
		private var _faces : Array;
		private var _alpha : Number;
		private var _normalMapStrength : Number;
		
		private var _hasNormalMap : Boolean;
		
		/**
		 * Create an EnvMapFilter instance.
		 * 
		 * @param faces An key-value array of BitmapData objects. Use the constants LEFT, RIGHT, TOP, BOTTOM, BACK, FRONT as keys.
		 * @param alpha The opacity of the environment map, ie: how reflective the surface is. 1 is a perfect mirror.
		 * @param normalMap A normal map in tangent space. If not provided, the surface will appear flat.
		 * @param normalMapStrength Indicates how strong the normal map affects the surface relief.
		 * 
		 * @see com.derschmale.stok3d.filters.EnvMapFaces
		 */
		public function EnvMapFilter(faces : Array, alpha : Number = 0.5, normalMap : BitmapData = null, normalMapStrength : Number = 0.5)
		{
			var shader : Shader;
			_hasNormalMap = normalMap != null;
			shader = _hasNormalMap ? new Shader(new NormalMapKernel()) : new Shader(new FlatKernel())
			super(shader);

			_alpha = alpha;
			_faces = faces;
			_normalMapStrength = normalMapStrength;
			shader.data.left.input = faces[EnvMapFaces.LEFT];
			shader.data.right.input = faces[EnvMapFaces.RIGHT];
			shader.data.top.input = faces[EnvMapFaces.TOP];
			shader.data.bottom.input = faces[EnvMapFaces.BOTTOM];
			shader.data.front.input = faces[EnvMapFaces.FRONT];
			shader.data.back.input = faces[EnvMapFaces.BACK];
			shader.data.cubeDim.value = [ faces[EnvMapFaces.LEFT].width*.5 ];
			shader.data.alpha.value = [ alpha ];
			
			if (_hasNormalMap) {
				shader.data.normalMap.input = normalMap;
				shader.data.normalMapStrength.value = [ normalMapStrength ];
			}
		}
		
		/**
		 * The opacity of the environment map, ie: how reflective the surface is. 1 is a perfect mirror.
		 */
		public function get alpha() : Number
		{
			return _alpha;
		}
		
		public function set alpha(value : Number) : void
		{
			_alpha = value;
			shader.data.alpha.value = [ value ];
		}
		
		/**
		 * Indicates how strong the normal map affects the surface relief.
		 */
		public function get normalMapStrength() : Number
		{
			return _normalMapStrength;
		}
		
		public function set normalMapStrength(value : Number) : void
		{
			if (_hasNormalMap) {
				_normalMapStrength = value;
				shader.data.normalMapStrength.value = [ value ];
			}
		}
	}
}